'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

''' <summary>
''' This ViewModel features selecting an image from a list of images.
''' </summary>
Public Class ImageSelectorViewModel
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _ImageFiles As List(Of String)
    Private _SelectedImageFile As String
    Private _DataDirectory As System.IO.DirectoryInfo

    Public Sub New(ByVal dataDirectory As System.IO.DirectoryInfo)
        _DataDirectory = dataDirectory
        LoadImageFiles()
    End Sub

    ''' <summary>
    ''' Gets a list of image file paths which a user can pick from.
    ''' </summary>
    Public ReadOnly Property ImageFiles() As List(Of String)
        Get
            Return _ImageFiles
        End Get
    End Property

    ''' <summary>
    ''' Gets or sets the currently selected image file path.
    ''' </summary>
    Public Property SelectedImageFile() As String
        Get
            Return _SelectedImageFile
        End Get
        Set(ByVal value As String)
            _SelectedImageFile = value
            OnPropertyChanged("SelectedImageFile")
        End Set
    End Property

    ''' <summary>
    ''' Finds image files in the data directory and adds them to ImageFiles.
    ''' </summary>
    Public Sub LoadImageFiles()
        Dim avatarDirectoryPath As String
        Dim avatarDirectory As System.IO.DirectoryInfo

        avatarDirectoryPath = System.IO.Path.Combine(_DataDirectory.FullName, "avatars")
        avatarDirectory = New System.IO.DirectoryInfo(avatarDirectoryPath)

        If Not avatarDirectory.Exists Then avatarDirectory.Create()

        _ImageFiles = (From file In avatarDirectory.GetFiles Select file.FullName).ToList

        OnPropertyChanged("ImageFiles")
    End Sub

    ''' <summary>
    ''' Launches an FileDialog allowing the user to specify a new image which will
    ''' be added to the data directory.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub AddImageFile()
        Dim dialog = New Microsoft.Win32.OpenFileDialog

        dialog.CheckFileExists = True
        dialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

        If dialog.ShowDialog Then
            Dim imageFile As System.IO.FileInfo
            Dim avatarDirectoryPath As String
            Dim avatarDirectory As System.IO.DirectoryInfo

            imageFile = New System.IO.FileInfo(dialog.FileName)
            avatarDirectoryPath = System.IO.Path.Combine(ApplicationService.Current.DataStorageDirectory.FullName, "avatars")
            avatarDirectory = New System.IO.DirectoryInfo(avatarDirectoryPath)

            Dim newPath As String = System.IO.Path.Combine(avatarDirectory.FullName, Guid.NewGuid.ToString & imageFile.Extension)

            imageFile.CopyTo(newPath)

            LoadImageFiles()
        End If
    End Sub

    Protected Sub OnPropertyChanged(ByVal propertyName As String)
        RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(propertyName))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class